package com.example.security;

import com.example.feign.UsernameFeignClient;
import com.example.model.api.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Slf4j
@Service("UserDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UsernameFeignClient usernameFeignClient;

    @Override
//    @Cacheable(value = "userDetails", key = "#username", unless = "#result == null")
    public UserDetail loadUserByUsername(String username) throws UsernameNotFoundException {
        Result<UserDetail> byUsername = usernameFeignClient.getByUsername(username);
        if (!byUsername.getCode().equals(Result.SUCCESS_CODE)) {
            throw new UsernameNotFoundException(byUsername.getMessage());
        }
        return byUsername.getData();
    }
} 